package progress.exa9_1;

import java.util.Collection;
import java.util.Iterator;

/**
 * 数组自定义具体实现
 * @param <E>
 */
public class ArraySelfList<E> extends AbstractionSelfList<E> {

     int size = 0;

     Object[] elementData;

     public String toString(){
         StringBuilder builder = new StringBuilder();
         builder.append("[");
         for(Object e:elementData){
             if(e!=null){
                 builder.append(e.toString()).append(",");
             }
         }
         builder.replace(builder.length()-1,builder.length(),"");
         builder.append("]");
         return builder.toString();
     }


     public ArraySelfList(){
         elementData = new Object[16];
     }

     public ArraySelfList(int capacity){
         elementData = new Object[capacity];
     }


    @Override
    public void addHead(E e) {
         //如果是第一次添加
         if(size==0){
             elementData[0] = e;
         } else if(size<elementData.length){
             Object[] temp = new Object[elementData.length];
             System.arraycopy(elementData,0,temp,1,size);
             temp[0] = e;
             elementData = temp;
         }else{
             Object[] temp = new Object[elementData.length+5];
             System.arraycopy(elementData,0,temp,1,size);
             temp[0] = e;
             elementData = temp;
         }
        size++;
    }

    @Override
    public void addTail(E e) {
       if(size<elementData.length){
            elementData[size++] = e;
        }else{
            Object[] temp = new Object[elementData.length+5];
            System.arraycopy(elementData,0,temp,0,size);
            temp[size++] = e;
            elementData = temp;
        }
    }

    @Override
    public E delete(E e) {
        return null;
    }

    @Override
    public E get(int i) {
         if(i<size)
           return (E)elementData[i];
         else
             throw new ArrayIndexOutOfBoundsException();
    }

    @Override
    public E getHead() {
        return null;
    }

    @Override
    public E getTail() {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }


}
